
#include "triu_tiling.h"
#include "register/op_def_registry.h"
#include "tiling/platform/platform_ascendc.h"
#include <algorithm>

namespace optiling {
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{
  TriuTilingData tiling;

    auto ascendcPlatform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo());
    auto aivNum = ascendcPlatform.GetCoreNum(); 
    
    auto shape_x = context->GetInputTensor(0)->GetOriginShape();
    int64_t M = 1, N = 1, Z = 1;

    if(shape_x.GetDimNum() > 2)
    {
        for (int i = 0; i < shape_x.GetDimNum() - 2; i++)
            Z *= shape_x.GetDim(i);
        M = shape_x.GetDim(shape_x.GetDimNum() - 2);
        N = shape_x.GetDim(shape_x.GetDimNum() - 1);
    }
    else
    {
        M = shape_x.GetDim(0);
        N = shape_x.GetDim(1);
        Z = 1;
    }
    tiling.set_M(M);
    tiling.set_N(N);
    tiling.set_Z(Z);

    int64_t diagonal = *context->GetAttrs()->GetInt(0);
    tiling.set_diagonal(diagonal);

  context->SetBlockDim(1);
  tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
  context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());

  return ge::GRAPH_SUCCESS;
}
}


namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
}


namespace ops {
class Triu : public OpDef {
public:
    explicit Triu(const char* name) : OpDef(name)
    {
        this->Input("x")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("y")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Attr("diagonal").AttrType(OPTIONAL).Int(0);

        this->SetInferShape(ge::InferShape);

        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend310b");

    }
};

OP_ADD(Triu);
}
